package com.softeem.music_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.PlayRecord;
import com.softeem.music_sys.service.PlayRecordService;
import com.softeem.music_sys.mapper.PlayRecordMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author RD
* @description 针对表【play_record(用户播放记录)】的数据库操作Service实现
* @createDate 2025-11-07 11:19:17
*/
@Service
public class PlayRecordServiceImpl extends ServiceImpl<PlayRecordMapper, PlayRecord>
    implements PlayRecordService{

    @Override
    public Result addPlayRecord(PlayRecord playRecord) {
        // 设置播放开始时间
        if (playRecord.getStartTime() == null) {
            playRecord.setStartTime(new Date());
        }
        
        boolean result = this.save(playRecord);
        return result ? Result.success().setMessage("添加成功").setData(playRecord) : Result.fail("添加失败");
    }

    @Override
    public Result updatePlayRecord(PlayRecord playRecord) {
        PlayRecord exist = this.getById(playRecord.getId());
        if (exist == null) {
            return Result.fail("播放记录不存在");
        }
        
        // 设置播放结束时间
        if (playRecord.getEndTime() == null) {
            playRecord.setEndTime(new Date());
        }
        
        boolean result = this.updateById(playRecord);
        return result ? Result.success().setMessage("更新成功") : Result.fail("更新失败");
    }

    @Override
    public Result deletePlayRecord(Integer id) {
        PlayRecord playRecord = this.getById(id);
        if (playRecord == null) {
            return Result.fail("播放记录不存在");
        }
        boolean result = this.removeById(id);
        return result ? Result.success().setMessage("删除成功") : Result.fail("删除失败");
    }

    @Override
    public Page<PlayRecord> getRecordsByUser(Integer userId, Integer page, Integer limit) {
        Page<PlayRecord> pageUtil = new Page<>(page, limit);
        QueryWrapper<PlayRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("start_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<PlayRecord> getRecordsByMusic(Integer musicId, Integer page, Integer limit) {
        Page<PlayRecord> pageUtil = new Page<>(page, limit);
        QueryWrapper<PlayRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("music_id", musicId);
        wrapper.orderByDesc("start_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public Page<PlayRecord> getRecentRecordsByUser(Integer userId, Integer limit) {
        Page<PlayRecord> pageUtil = new Page<>(1, limit);
        QueryWrapper<PlayRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.orderByDesc("start_time");
        return page(pageUtil, wrapper);
    }

    @Override
    public long countPlayTimes(Integer userId, Integer musicId) {
        QueryWrapper<PlayRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("music_id", musicId);
        return count(wrapper);
    }

    @Override
    public Result clearUserRecords(Integer userId) {
        QueryWrapper<PlayRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        boolean result = remove(wrapper);
        return result ? Result.success().setMessage("清空成功") : Result.fail("清空失败");
    }

    @Override
    public Page<PlayRecord> getRecordsByTimeRange(Integer userId, String startDate, String endDate, Integer page, Integer limit) {
        Page<PlayRecord> pageUtil = new Page<>(page, limit);
        QueryWrapper<PlayRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.between("start_time", startDate, endDate);
        wrapper.orderByDesc("start_time");
        return page(pageUtil, wrapper);
    }
}